package com.gimi.cloud.jimi.gateway.service.impl;

import com.gimi.cloud.jimi.common.enums.common.RedisKeyEnum;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.dao.dao.sys.SysMasterDao;
import com.gimi.cloud.jimi.dao.redis.IRedisService;
import com.gimi.cloud.jimi.dto.sys.SysMasterDTO;
import com.gimi.cloud.jimi.gateway.service.CheckMasterService;
import org.springframework.stereotype.Service;

/**
 * Created by shazhiwei on 2018/1/2.
 */
@Service
public class CheckMasterServiceImpl implements CheckMasterService {
    private final SysMasterDao masterDao;
    private final IRedisService redisService;

    public CheckMasterServiceImpl(SysMasterDao masterDao,IRedisService redisService){
        this.masterDao = masterDao;
        this.redisService = redisService;
    }

    @Override
    public SysMasterDTO getMasterByUserName(String userName) {
        SysMasterDTO result = null;
        String redisKey = RedisKeyEnum.JIMI_MASTER_PRE.getKey() + userName;
        String master = redisService.get(redisKey);
        if(StringUtil.isBlank(master)){
            result = masterDao.findByUserName(userName);
            if(result != null){
                redisService.set(redisKey, JsonUtil.toJson(result));
            }
        }else{
            result = JsonUtil.jsonToBean(master,SysMasterDTO.class);
        }
        return result;
    }

}
